/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.dew.sisrest.dao.hibernate;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import pe.edu.upc.dew.sisrest.dao.IngredienteDao;
import pe.edu.upc.dew.sisrest.model.Ingrediente;

/**
 *
 * @author Aldo
 */
@Repository
public class IngredienteDaoHibernate extends HibernateDaoSupport implements IngredienteDao {

    @Autowired
    public IngredienteDaoHibernate(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Ingrediente> obtenerIngredientes() {
        /*StringBuffer query = new StringBuffer("from Ingrediente where 1 = 1 ");

        if (menu.getNombre() != null
                && !menu.getNombre().isEmpty()) {
            query.append(" and upper(m.nombre) like '%").append(menu.getNombre().toUpperCase()).append("%'");
        }

        if (menu.getTipoMenu().getIdTipoMenu() != null) {
            query.append(" and m.tipoMenu.idTipoMenu = ").append(menu.getTipoMenu().getIdTipoMenu());
        }
         *
         */
        
        return getHibernateTemplate().find("from Ingrediente");
    }

    @Override
    public Ingrediente obtenerIngrediente(String sId) {
        return (Ingrediente) getSession().createQuery("from Ingrediente "
                + "where iding=?").setString(sId, sId).uniqueResult();
    }

    @Override
    public void grabarIngrediente(Ingrediente objIng) {
        getHibernateTemplate().saveOrUpdate(objIng);
    }

    @Override
    public void eliminarIngrediente(String sId) {
        Ingrediente oIngrediente = obtenerIngrediente(sId);
        getHibernateTemplate().delete(oIngrediente);
    }
}
